package com.memory.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.monitor.annotation.AuthCheck;
import com.memory.monitor.common.BaseResponse;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ResultUtils;
import com.memory.monitor.constant.UserConstant;
import com.memory.monitor.model.dto.distribute.DistributeAddRequest;
import com.memory.monitor.model.dto.distribute.DistributeQueryRequest;
import com.memory.monitor.model.dto.distribute.DistributeUpdateRequest;
import com.memory.monitor.model.entity.Distribute;
import com.memory.monitor.service.DistributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/distribute")
@Slf4j
public class DistributeController {
    @Resource
    private DistributeService distributeService;

    /**
     * 分页获取信息
     *
     * @param distributeQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Distribute>> listByPageDistribute(@RequestBody DistributeQueryRequest distributeQueryRequest) {
        long current = distributeQueryRequest.getCurrent();
        long size = distributeQueryRequest.getPageSize();
        // Page<User> userPage = userService.page(new Page<>(current, size),
        //         userService.getQueryWrapper(userQueryRequest));
        Page<Distribute> userPage = distributeService.page(new Page<>(current, size));

        return ResultUtils.success(userPage);
    }

    /**
     * 增加信息 返回id
     *
     * @param distributeAddRequest /**
     *                             增加信息 返回id
     * @param distributeAddRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addDistribute(@RequestBody DistributeAddRequest distributeAddRequest) {
        Long id = distributeService.addDistribute(distributeAddRequest);
        return ResultUtils.success(id);
    }

    /**
     * 修改信息 返回boolean
     *
     * @param distributeUpdateRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateDistribute(@RequestBody DistributeUpdateRequest distributeUpdateRequest) {
        Boolean result = distributeService.updateDistribute(distributeUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除信息 返回boolean
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteDistribute(@RequestBody DeleteRequest deleteRequest) {
        Boolean result = distributeService.deleteDistribute(deleteRequest);
        return ResultUtils.success(result);
    }
}
